/**
 * 
 */
package com.hk.Mgr.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.department.DutiesReq;
import com.hk.Mgr.web.message.department.DutiesRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.Department;
import com.hk.commons.entity.Duties;
import com.hk.service.sys.IDepartment;
import com.hk.service.sys.IDuties;

/**
 * @author psx 职务实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/DutiesCtrl")
public class DutiesCtrl extends BaseCtrl {

	@Autowired
	private IDuties dutiesService;

	@Autowired
	private IDepartment departmentService;
	
	/**
	 * @Title: getDutiesById
	 * @Description: 根据id获取系统模块信息
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getDutiesById", method = RequestMethod.POST)
	public ResponseBase<DutiesRes> getDutiesById(
			@RequestBody DutiesReq req) {
		ResponseBase<DutiesRes> resp = new ResponseBase<DutiesRes>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			Duties duties = dutiesService.findById(req.getId());

			DutiesRes singleResp = new DutiesRes();
			ReqToModelHelper.copyReqValueToModel(duties, singleResp);
			String deptName="";
			if(duties.getDeptId()!=null){
				Department dept=departmentService.findById(duties.getDeptId());
				if(dept!=null)
					deptName=dept.getName();
			}
			singleResp.setDeptName(deptName);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setResult(singleResp);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: getDutiesList
	 * @Description: 分页获取系统模块
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getDutiesList", method = RequestMethod.POST)
	public ResponseBase<List<DutiesRes>> getDutiesList(
			@RequestBody DutiesReq req) {
		ResponseBase<List<DutiesRes>> resp = new ResponseBase<List<DutiesRes>>();
		HqlFilter hqlFilter = new HqlFilter();
		if (StringUtils.isNotEmpty(req.getName())) {
			hqlFilter.addLKFilter("name", req.getName());
		}
		if (req.getDeptId()!=null) {
			hqlFilter.addEQFilter("deptId", req.getDeptId());
		}
		hqlFilter.addSort("id");
		hqlFilter.addOrder("desc");
		try {
			List<Duties> dutiesList = dutiesService.findByFilter(
					hqlFilter, req.getCurPage(), req.getPageSize());

			List<DutiesRes> respList = new ArrayList<DutiesRes>();
			// 根据模块id查询资源
			if (dutiesList != null && !dutiesList.isEmpty()) {
				DutiesRes singleResp = null;
				for (Duties duties : dutiesList) {
					singleResp = new DutiesRes();
					ReqToModelHelper
							.copyReqValueToModel(duties, singleResp);
					String deptName="";
					if(duties.getDeptId()!=null){
						Department dept=departmentService.findById(duties.getDeptId());
						if(dept!=null)
							deptName=dept.getName();
					}
					singleResp.setDeptName(deptName);
					
					respList.add(singleResp);
				}
			}
			Long total =dutiesService.countByFilter(hqlFilter);
			resp.setSize(total);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setResult(respList);

		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: saveDuties
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/saveDuties", method = RequestMethod.POST)
	public ResponseBase<Duties> saveDuties(
			@RequestBody DutiesReq req) {
		ResponseBase<Duties> resp = new ResponseBase<Duties>();
		try {
			Duties model = new Duties();
			model.setCreatedts(this.getCurrentTime());
			model.setCreator(this.getAccount());
			model.setName(req.getName());
			model.setDeptId(req.getDeptId());
			dutiesService.add(model);

			resp.setResult(model);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("添加成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: updateDuties
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/updateDuties", method = RequestMethod.POST)
	public ResponseBase<Duties> updateDuties(@RequestBody DutiesReq req) {
		ResponseBase<Duties> resp = new ResponseBase<Duties>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			Duties model = dutiesService.findById(req.getId());
			model.setName(req.getName());
			model.setDeptId(req.getDeptId());
			dutiesService.update(model);

			resp.setResult(model);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("修改成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: deleteDuties
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteDuties", method = RequestMethod.POST)
	public ResponseBase<Duties> deleteDuties(@RequestBody DutiesReq req) {
		ResponseBase<Duties> resp = new ResponseBase<Duties>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}

			Duties ret = dutiesService.delete(req.getId());

			resp.setResult(ret);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("删除成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
}
